#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.forms.fields import CharField
from django import forms
import string

class FullNameField(CharField):
    def __init__(self, **kwargs):
        super(FullNameField, self).__init__(**kwargs)
        
    def clean(self, full_name):
        """
        Make sure the lecturer is given seperated by a , or by first_name[space]last_name
        """
        first_name, last_name = FullNameField.seperate_full_name(full_name)
        
        if(first_name == '' or last_name == ''):
            raise forms.fields.ValidationError("Format falsch.")

        return first_name, last_name
    
    @staticmethod
    def seperate_full_name(full_name):
        """
        Seperates the lecturer into (first_name, last_name) - only last_name if there is neither a space nor a comma available
        
        Keyword arguments:
        lecturer -- string of a lecturer's name either 
                    [space]*last_name[space]*,[space]*firstlast_name[space]*
                    or
                    [space]*first_name[space]*last_name[space]*
        """
        
        full_name = string.strip(full_name)
        comma_name = full_name.rpartition(',')
        if(comma_name[1] == ''):
            space_lecturer = full_name.rpartition(' ')
            first_name = space_lecturer[0]
            last_name = space_lecturer[2]
        else:
            first_name = comma_name[2]
            last_name = comma_name[0]
        
        return string.strip(first_name), string.strip(last_name)